package com.tnxst.phexamination.activity;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import androidx.lifecycle.SavedStateViewModelFactory;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.tnxst.phexamination.R;
import com.tnxst.phexamination.adapter.SignServiceAdapter;
import com.tnxst.phexamination.constant.HISConfig;
import com.tnxst.phexamination.databinding.ActivitySignManagementBinding;
import com.tnxst.phexamination.dto.DoctorDTO;
import com.tnxst.phexamination.dto.ServicePackageDTO;
import com.tnxst.phexamination.dto.SignDTO;
import com.tnxst.phexamination.fragment.SignFragment;
import com.tnxst.phexamination.public_health_docking_interface.FamilySignInterface;
import com.tnxst.phexamination.utils.ReflectUtils;
import com.tnxst.phexamination.utils.StringUtils;
import com.tnxst.phexamination.utils.Utils;
import com.tnxst.phexamination.utils.WebServiceUtils;
import com.tnxst.phexamination.viewmodel.GlobalViewModel;
import com.tnxst.phexamination.vo.GetSignVo;
import com.tnxst.phexamination.vo.TeamVo;
import com.tnxst.phexamination.vo.UserInfoVo;

import org.angmarch.views.NiceSpinner;
import org.angmarch.views.OnSpinnerItemSelectedListener;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class SignManagementActivity extends BaseActivity{

    private GlobalViewModel viewModel;

    private Context context;

    private ActivitySignManagementBinding binding;

    private DoctorDTO userInfo;

    private List<SignDTO> dtos;

    private Integer pageSize = 1000;

    private SignServiceAdapter signServiceAdapter;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        viewModel = new ViewModelProvider(this, new SavedStateViewModelFactory(getApplication(), this)).get(GlobalViewModel.class);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_sign_management);
        binding.setSignManagement(viewModel);
        binding.setLifecycleOwner(this);
        context = this;
        userInfo = viewModel.getUserInfo();
        initData();
        intView();
    }

    private void intView() {
        binding.niceSpinner.setOnSpinnerItemSelectedListener(new OnSpinnerItemSelectedListener() {
            @Override
            public void onItemSelected(NiceSpinner parent, View view, int position, long id) {
                int status = 0;
                switch (position){
                    case 0:
                        status = 2;
                        break;
                    case 1:
                        status = 4;
                        break;
                    case 2:
                        status = 5;
                        break;
                }
//                getSignDTOByNetwork(status,pageSize);
            }

        });
        binding.imageView6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Fragment fragment;
                SignDTO signDTO = new SignDTO();
                FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
                ft.setCustomAnimations(R.anim.fragment_slide_up, 0);
                fragment = SignFragment.newInstance();
                Bundle bundle = new Bundle();
                bundle.putSerializable("signDTO", signDTO);
                fragment.setArguments(bundle);
                ft.replace(android.R.id.content, fragment);
                ft.show(fragment);
                ft.commitAllowingStateLoss();
            }
        });
    }

    private void initData() {
        LinkedList<String> strings = new LinkedList<>();
        strings.add("已通过");
        strings.add("终止");
        strings.add("解约");
        binding.niceSpinner.attachDataSource(strings);
        binding.niceSpinner.setSelectedIndex(0);
        dtos = viewModel.getSignRepository().findByFieldList("STATUS",2);
        signServiceAdapter = new SignServiceAdapter(dtos,getSupportFragmentManager());
        binding.recycler.setAdapter(signServiceAdapter);
        binding.recycler.setLayoutManager(new LinearLayoutManager(context));
    }




//    public void getSignDTOByNetwork(Integer status,Integer pageIndex){
//        if (StringUtils.isNotNull(HISConfig.PRODUCT_CODE)){
//            List<TeamVo> teamVos = userInfo.getTeamVos();
//            if (teamVos.size() > 0){
//                for (TeamVo teamVo : teamVos) {
//                    GetSignVo getSignVo = new GetSignVo();
//                    getSignVo.setTeamId(teamVo.getTEAMID());
//                    getSignVo.setStatus(status);
//                    FamilySignInterface.getSign(pageSize, pageIndex, getSignVo, new WebServiceUtils.Response<List>() {
//                        @Override
//                        public void onSuccess(List result) {
//                            List<SignDTO> signDTOS = new ArrayList<>();
//                            for (Object o : result) {
//                                Map data = (Map) o;
//                                SignDTO signDTO = (SignDTO) ReflectUtils.mapToObject(data, SignDTO.class);
//                                signDTO.setUpload(true);
//                                LinkedHashMap<String, Object> map = new LinkedHashMap<>();
//                                map.put("upload",false);
//                                map.put("CONTRACTID",signDTO.getCONTRACTID());
//                                if (viewModel.getSignRepository().findByMap(map) == null)
//                                    signDTOS.add(signDTO);
//                            }
//                            viewModel.getSignRepository().insert(signDTOS.toArray(new SignDTO[signDTOS.size()]));
//                            dtos = viewModel.getSignRepository().findByFieldList("STATUS",status);
//                            signServiceAdapter.setSignDTOS(dtos);
//                            signServiceAdapter.notifyDataSetChanged();
//                            if (result.size() >= pageSize)
//                                getSignDTOByNetwork(status,pageIndex+1);
//                        }
//
//                        @Override
//                        public void onError(String error) {
//                            Utils.Toast(context,"获取签约数据失败！\n"+error);
//                        }
//                    });
//                }
//            }
//        }else {
//            dtos = viewModel.getSignRepository().findByFieldList("STATUS",status);
//            signServiceAdapter.setSignDTOS(dtos);
//            signServiceAdapter.notifyDataSetChanged();
//        }
//    }


}
